<template>
  <section>
    <nb-table
      operator
      :tableData="tableData"
      :tableHeaders="tableHeaders"
      :allField="allField"
      :field="field"
      @saveField="saveField"
      @resetField="resetField"
      :queryModel='searchModel'
      :headerMap='queryMap'
      @selectRow="selectRow"
      height="auto"
      :clickData="clickData"
      operatorFixed="left"
      select="radio"
    ></nb-table>
    <pagination
      :total="total"
      :model="searchModel"
      :fetch="search"
    ></pagination>
  </section>
</template>

<script>
  import NbTable from '@/components/nb-table.vue'
  import Pagination from '@/components/pagination.vue'
  import {map} from 'lodash'
  import MokeDate from './template/moke'
  import MokeDFields from './template/mokefields'
  export default {
    data () {
      return {
        total: 0,
        searchModel: {
          pageNo: 1,
          pageSize: 20,
          orderBy: '',
          param: {
            status: this.status,
            name: '',
            typeId: '',
            activeStatus: '',
            managerName: '',
            deptName: '',
            beginStartTime: '',
            finishStartTime: '',
            beginEndTime: '',
            finishEndTime: ''
          }
        },
        clickData: [{
          name: '查看员工',
          type: 'look'
        }, {
          name: '修改',
          type: 'edit'
        }, {
          name: '删除',
          type: 'del'
        }],
        queryMap: [{
          headerKey: 'name',
          headerType: 'sort',
          headerName: '产品名称',
          click: true,
          fixed: true
        }, {
          headerKey: 'categoryName',
          headerName: '产品分类',
          fixed: true
        }, {
          headerKey: 'recommendStr',
          width: '140',
          headerType: 'sort'
        }, {
          headerKey: 'deptName',
          headerType: 'input',
          headerName: '产品名称'
        }, {
          headerKey: 'term',
          width: '170',
          headerName: '产品期限'
        }, {
          headerKey: 'actualCost'
        }, {
          headerKey: 'releaseTime',
          width: '170'
        }, {
          headerKey: 'scopeStr',
          width: '120'
        }, {
          headerKey: 'readNum',
          width: '120'
        }, {
          headerKey: 'signNum',
          width: '120'
        }, {
          headerKey: 'signupNum',
          width: '120'
        }, {
          headerKey: 'actualCost',
          width: '180'
        }],
        tableData: MokeDate.data.tableData,
        tableHeaders: MokeDate.data.tableHeaders,
        costVisible: false,
        actList: [],
        start: '',
        end: '',
        allField: MokeDFields.data,
        field: [],
        row: null
      }
    },
    props: {
      status: String
    },
    watch: {
      status () {
        this.searchModel.param.status = this.status
      }
    },
    components: {
      NbTable,
      Pagination
    },
    methods: {
      handleSearch (value, row, type) {
       // if (type === 'sort') {
       //   this.searchModel.orderBy = value === 1 ? `order by ${row.headerKey} desc` : ''
       // }
        if (this.searchModel.param.hasOwnProperty(row.searchKey || row.headerKey)) {
          this.searchModel.param[row.searchKey || row.headerKey] = value
        }
        this.searchModel.param.status = this.status
        this.total = 0
        this.searchModel.pageNo = 1
        this.search()
      },
      delQueryModel (data, item) {
        this.searchModel = data
        if (item.type === 'daterange') {
          if (item.key === 'endTime') {
            this.clearEndTime()
          }
          if (item.key === 'startTime') {
            this.clearStartTime()
          }
        } else if (item.type === 'all') {
          this.clearEndTime()
          this.clearStartTime()
        }
        this.total = 0
        this.searchModel.pageNo = 1
        this.search(this.searchModel)
      },
      clearStartTime () {
        this.searchModel.param.beginStartTime = ''
        this.searchModel.param.finishStartTime = ''
      },
      clearEndTime () {
        this.searchModel.param.beginEndTime = ''
        this.searchModel.param.finishEndTime = ''
      },
      async search (data, type) {
        await this.$store.dispatch('MARKET_SEARCH', data || this.searchModel).then(res => {
          if (res.success) {
            this.tableData = res.data.tableDataValues
            this.tableHeaders = res.data.tableHeaders
            this.total = this.total ? this.total : res.data.totalCount
            this.$emit('over')
          } else {
            this.$message.error(res.msg)
          }
        })
        if (type) {
          await this.$store.dispatch('GET_ALL_FIELD', {param: {value: 1}}).then(res => {
            if (res.success) {
              this.allField = res.data
              this.field = this.allField.filter(item => {
                return map(this.tableHeaders, 'headerKey').indexOf(item.propName) !== -1
              })
            }
          })
        }
      },
      costOver () {
        this.$emit('costOver')
      },
      saveField (data) {
        this.$store.dispatch('CUSTOM_FIELD', {param: {
          objType: 1,
          objPropIds: data
        }}).then(res => {
          if (res.success) {
            this.$message.success('修改成功！')
            this.search('', true)
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      resetField () {
        this.$store.dispatch('RESET_CUSTOM_FIELD', {
          param: {
            value: 1
          }
        }).then(res => {
          if (res.success) {
            this.$message.success('恢复默认设置成功！')
            this.search('', true)
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      query () {
        this.searchModel.param.beginStartTime = this.start[0] || ''
        this.searchModel.param.finishStartTime = this.start[0] || ''
        this.searchModel.param.beginEndTime = this.end[0] || ''
        this.searchModel.param.finishEndTime = this.end[0] || ''
        this.search()
      },
      resetSearch () {
        this.reset(this.$refs.search.$children[0])
        this.searchModel.param.beginStartTime = ''
        this.searchModel.param.finishStartTime = ''
        this.searchModel.param.beginEndTime = ''
        this.searchModel.param.finishEndTime = ''
        this.search()
      },
      change (index, type) {
        if (type) {
          this.searchModel.pageSize = index
          this.searchModel.pageNo = 1
        } else {
          this.searchModel.pageNo = index
        }
        this.search()
      },
      selectRow (selection, row) {
        this.$emit('batchData', selection.length, row)
      },
      clickRight (data) {
        switch (data.type) {
          case 'back':
            this.back(data.row)
            break
        }
      }
    },
    mounted () {
    }
  }
</script>

<style lang="scss">
</style>
